/**
 * SettingsPane.java
 * A frame of changeable program settings.
 * 
 * Copyright (c) 2012 The Javanauts
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package edu.utsa.cs4953.abacus;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

/**
 * Lets the user change the program settings.
 * 
 */
public class SettingsPane extends JFrame {
	/**
	 * Included because JFrame implements the Serializable interface.
	 */
	private static final long serialVersionUID = 3368918122632941567L;

	/**
	 * Default Constructor.
	 */
	public SettingsPane() {

		setTitle("Settings");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

		// Switch between addition mode and multiplication mode
		JRadioButton addLayout = new JRadioButton("Normal", true);
		addLayout.addActionListener(new MenuHandler());
		addLayout.setSelected(Settings.getMode() == Mode.NORMAL);
		addLayout.setActionCommand("Addition");
		JRadioButton multLayout = new JRadioButton("Split", false);
		multLayout.addActionListener(new MenuHandler());
		multLayout.setSelected(Settings.getMode() == Mode.SPLIT);
		multLayout.setActionCommand("Multiplication");

		ButtonGroup layoutToggleGroup = new ButtonGroup();
		layoutToggleGroup.add(addLayout);
		layoutToggleGroup.add(multLayout);

		JPanel layoutTogglePane = new JPanel();
		layoutTogglePane.setLayout(new GridLayout(1, 2));
		layoutTogglePane.add(addLayout);
		layoutTogglePane.add(multLayout);

		layoutTogglePane.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Layout"));

		contentPane.add(layoutTogglePane);

		// Toggle showing Arabic digits
		JCheckBox mayanOn = new JCheckBox("Mayan Numerals");
		mayanOn.addActionListener(new MenuHandler());
		mayanOn.setSelected(Settings.getMayaPanel().isVisible());
		mayanOn.setActionCommand("Toggle Mayan Numerals");
		JCheckBox decimalOn = new JCheckBox("Decimal Numerals");
		decimalOn.addActionListener(new MenuHandler());
		decimalOn.setSelected(Settings.getStatusPanel().isVisible());
		decimalOn.setActionCommand("Toggle Decimal Numerals");

		JPanel numeralTogglePane = new JPanel();
		numeralTogglePane.setLayout(new GridLayout(1, 2));
		numeralTogglePane.add(decimalOn);
		numeralTogglePane.add(mayanOn);

		numeralTogglePane.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Numeral Display"));
		contentPane.add(numeralTogglePane);

		JComboBox themeSelector = new JComboBox(Settings.getAllThemes());
		themeSelector.setSelectedIndex(Settings.getCurrentThemeOrdinal());
		themeSelector.addActionListener(new ThemeHandler());

		layoutTogglePane = new JPanel();
		layoutTogglePane.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Theme"));
		layoutTogglePane.setLayout(new GridLayout(1, 2));
		contentPane.add(layoutTogglePane);
		layoutTogglePane.add(themeSelector);

		
		// Switch between addition mode and multiplication mode
		JRadioButton click = new JRadioButton("Click", true);
		click.addActionListener(new MenuHandler());
		click.setSelected(Settings.getCurrentSoundEffect() == SoundEffect.CLICK);
		click.setActionCommand("Sound Click");
		JRadioButton blip = new JRadioButton("Blip", false);
		blip.addActionListener(new MenuHandler());
		blip.setSelected(Settings.getCurrentSoundEffect() == SoundEffect.BLIP);
		blip.setActionCommand("Sound Blip");
		JRadioButton noSound = new JRadioButton("Off", false);
		noSound.addActionListener(new MenuHandler());
		noSound.setSelected(Settings.getCurrentSoundEffect() == SoundEffect.NONE);
		noSound.setActionCommand("Sound Off");

		ButtonGroup soundToggleGroup = new ButtonGroup();
		soundToggleGroup.add(click);
		soundToggleGroup.add(blip);
		soundToggleGroup.add(noSound);

		numeralTogglePane = new JPanel();
		numeralTogglePane.setLayout(new GridLayout(1, 3));
		numeralTogglePane.add(click);
		numeralTogglePane.add(blip);
		numeralTogglePane.add(noSound);
		numeralTogglePane.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Sound Effects"));
		contentPane.add(numeralTogglePane);

		// Check box showing trivia.
		JCheckBox trivia = new JCheckBox("Enable Trivia on Startup");
		trivia.addActionListener(new MenuHandler());
		trivia.setSelected(Settings.isShowTrivia());

		numeralTogglePane = new JPanel();
		numeralTogglePane.setLayout(new GridLayout(1, 2));
		numeralTogglePane.add(trivia);
		numeralTogglePane.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Show Trivia"));
		contentPane.add(numeralTogglePane);

		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new MenuHandler());
		contentPane.add(saveButton);
		pack();
	}
}
